package com.jiangyao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangyao.common.handler.WebContextHolder;
import com.jiangyao.common.utils.Util;
import com.jiangyao.dao.ILoginLogDao;
import com.jiangyao.model.entity.LoginLog;
import com.jiangyao.service.LoginLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 用户登录日志 服务实现类
 * </p>
 *
 * @author yangyongchuan
 * @since 2020-08-13
 */
@Slf4j
@Service
public class LoginLogServiceImpl extends ServiceImpl<ILoginLogDao, LoginLog> implements LoginLogService {

	@Override
	public void saveLoginLog(LoginLog loginLog) {
		try {
			loginLog.setLoginTime(new Date());
			loginLog.setLoginIp(Util.getIpAddr());
			HttpServletRequest request = WebContextHolder.getRequest();
			loginLog.setRequestUserAgent(request.getHeader("User-Agent"));
			this.save(loginLog);
		} catch (Exception e) {
			log.error("保存登录日志失败，error:{}", e.getMessage(), e);
		}

	}
}
